<template>
  <div class="home">
    <el-row :gutter="20">
      <el-col :span="8">
        <el-card shadow="hover">
          <div class="user">
            <img :src="getImageUrl('user')" />
            <div class="user-info">
              <p>Admin</p>
              <p>超级管理员</p>
            </div>
          </div>
          <div class="login-info">
            <p>最新登录时间:<span>2025/9/24</span></p>
            <p>最新登录地点:<span>济南</span></p>
          </div>
        </el-card>
        <el-card class="user-table">
          <el-table :data="list">
            <el-table-column
              v-for="(item, index) in listLable"
              :key="index"
              :label="item.label"
              :prop="item.prop"
            ></el-table-column>
          </el-table>
        </el-card>
      </el-col>
      <el-col :span="16">
        <div class="num">
          <el-card
            :body-style="{ display: 'flex', padding: 0 }"
            v-for="item in countData"
            :key="item.name"
          >
            <component
              :is="item.icon"
              class="icons"
              :style="{ backgroundColor: item.color, fontSize: '20px' }"
            ></component>
            <div class="detail">
              <p>￥ {{ item.value }}</p>
              <p>{{ item.name }}</p>
            </div>
          </el-card>
        </div>
        <el-card>
          <div ref="chartRef" style="height: 200px"></div>
        </el-card>
        <div class="groups">
          <el-card>
            <div ref="userChart" style="height: 180px"></div>
          </el-card>
          <el-card>
            <div ref="pieChart" style="height: 180px"></div>
          </el-card>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script setup>
import { ref, onMounted, getCurrentInstance } from "vue";
import axios from "axios";
import * as echarts from "echarts";
import echartsOptions from "@/util/echarts.js";
import { getTableData, getCountData, getChartData } from "@/api/home.js";

// const { proxy } = getCurrentInstance();
const getImageUrl = () => {
  return new URL("../assets/images/user.png", import.meta.url).href;
};

const list = ref([]);
const countData = ref([]);
const chartRef = ref(null);
const listLable = ref([
  {
    label: "产品名称",
    prop: "name",
  },
  {
    label: "今日销售",
    prop: "totalBuy",
  },
  {
    label: "月销售",
    prop: "monthBuy",
  },
  {
    label: "总销售",
    prop: "totalBuy",
  },
]);
const userChart = ref(null);
const pieChart = ref(null);
const observe = ref(null);
const getTableDataFn = async () => {
  const tableData = await getTableData();
  list.value = tableData;
  // const tableData = await proxy.$api.getTableData();
  // list.value = tableData;
};

const getCountDataFn = async () => {
  const data = await getCountData();
  countData.value = data;
};

const getChartDataFn = async () => {
  const { orderData, userData, videoData } = await getChartData();
  let polylineChart = echarts.init(chartRef.value);
  echartsOptions.xoptions.xAxis.data = orderData.date;
  echartsOptions.xoptions.series = Object.keys(orderData.data[0]).map(
    (o, index) => ({
      name: o,
      data: orderData.data.map((item) => item[o]),
      type: "line",
    })
  );
  echartsOptions.xoptions.grid.left = "5%";
  polylineChart.setOption(echartsOptions.xoptions);

  let doubleBarChart = echarts.init(userChart.value);
  echartsOptions.xoptions.xAxis.data = userData.map((o) => o.date);
  echartsOptions.xoptions.series = [
    {
      name: "新增用户",
      data: userData.map((o) => o.new),
      type: "bar",
    },
    {
      name: "活跃用户",
      data: userData.map((o) => o.active),
      type: "bar",
    },
  ];
  doubleBarChart.setOption(echartsOptions.xoptions);

  let pieChartDom = echarts.init(pieChart.value);
  echartsOptions.pieoptions.series[0].data = videoData;
  pieChartDom.setOption(echartsOptions.pieoptions);

  // 实现页面监听
  observe.value = new ResizeObserver(() => {
    polylineChart.resize();
    doubleBarChart.resize();
    pieChartDom.resize();
  });

  if (chartRef.value) {
    observe.value.observe(chartRef.value);
  }
};

onMounted(() => {
  getTableDataFn();
  getCountDataFn();
  getChartDataFn();
});

// axios.get("/api/home/getData").then((res) => {
//   console.log(res);
//   list.value = res.data.data;
// });
</script>
<style lang="less" scoped>
.home {
  height: 100%;
  overflow: hidden;
  width: 100%;
}
.user {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  padding-bottom: 20px;
  margin-bottom: 20px;
  img {
    width: 150px;
    height: 150px;
    margin-right: 40px;
  }
  .user-info {
    p {
      line-height: 40px;
    }
    p:first-child {
      font-size: 38px;
    }
    p:nth-child(2) {
      color: #999;
    }
  }
}
.login-info {
  p {
    line-height: 30px;
    color: #ddd;
    font-size: 14px;
    span {
      color: #333;
      margin-left: 50px;
    }
  }
}
.user-table {
  margin-top: 20px;
}
.num {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: space-between;
  .icons {
    width: 60px;
    height: 60px;
    line-height: 60px;
    font-size: 20px;
    color: #fff;
  }
  .el-card {
    width: 32%;
    margin-bottom: 20px;
  }
  .detail {
    margin-left: 20px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    p:first-child {
      font-size: 24px;
      color: #333;
      font-weight: 600;
    }
    p:nth-child(2) {
      color: #ddd;
      font-size: 12px;
    }
  }
}
.groups {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
  .el-card {
    width: 48%;
  }
}
</style>
